home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / advanced97 / TEXGEN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  6.8 KB  |  293 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <GL/glut.h>
  5.  
  6. #ifndef __sgi
  7. /* Most math.h's do not define float versions of math functions. */
  8. #define floorf(x) ((float)floor((x)))
  9. #endif
  10.  
  11. static float transx = 1.0, transy, rotx, roty;
  12. static int ox = -1, oy = -1;
  13. static int mot = 0;
  14. #define PAN    1
  15. #define ROT    2
  16.  
  17. void
  18. pan(const int x, const int y) {
  19.     transx +=  (x-ox)/5.;
  20.     transy -= (y-oy)/5.;
  21.     ox = x; oy = y;
  22.     glutPostRedisplay();
  23. }
  24.  
  25. void
  26. rotate(const int x, const int y) {
  27.     rotx += x-ox;
  28.     if (rotx > 360.) rotx -= 360.;
  29.     else if (rotx < -360.) rotx += 360.;
  30.     roty += y-oy;
  31.     if (roty > 360.) roty -= 360.;
  32.     else if (roty < -360.) roty += 360.;
  33.     ox = x; oy = y;
  34.     glutPostRedisplay();
  35. }
  36.  
  37. void
  38. motion(int x, int y) {
  39.     if (mot == PAN) pan(x, y);
  40.     else if (mot == ROT) rotate(x,y);
  41. }
  42.  
  43. void
  44. mouse(int button, int state, int x, int y) {
  45.     if(state == GLUT_DOWN) {
  46.     switch(button) {
  47.     case GLUT_LEFT_BUTTON:
  48.         mot = PAN;
  49.         motion(ox = x, oy = y);
  50.         break;
  51.     case GLUT_MIDDLE_BUTTON:
  52.         mot = ROT;
  53.         motion(ox = x, oy = y);
  54.         break;
  55.     case GLUT_RIGHT_BUTTON:
  56.         break;
  57.     }
  58.     } else if (state == GLUT_UP) {
  59.     mot = 0;
  60.     }
  61. }
  62.  
  63. #define    stripeImageWidth 32
  64. GLubyte stripeImage[4*stripeImageWidth];
  65.  
  66. void makeStripeImage(void) {
  67.    int j;
  68.     
  69.    for (j = 0; j < stripeImageWidth; j++) {
  70.       stripeImage[4*j] = (GLubyte) ((j<=4) ? 255 : 0);
  71.       stripeImage[4*j+1] = (GLubyte) ((j>4) ? 255 : 0);
  72.       stripeImage[4*j+2] = (GLubyte) 0;
  73.       stripeImage[4*j+3] = (GLubyte) 255;
  74.    }
  75. }
  76.  
  77. void
  78. hsv_to_rgb(float h,float s,float v,float *r,float *g,float *b)
  79. {
  80.     int i;
  81.     float f, p, q, t;
  82.  
  83.     h *= 360.0;
  84.     if (s==0) {
  85.     *r = v;
  86.     *g = v;
  87.     *b = v;
  88.     } else {
  89.     if (h==360) 
  90.         h = 0;
  91.     h /= 60;
  92.     i = floorf(h);
  93.     f = h - i;
  94.     p = v*(1.0-s);
  95.     q = v*(1.0-(s*f));
  96.     t = v*(1.0-(s*(1.0-f)));
  97.     switch (i) {
  98.         case 0 : 
  99.         *r = v;
  100.         *g = t;
  101.         *b = p;
  102.         break;
  103.         case 1 : 
  104.         *r = q;
  105.         *g = v;
  106.         *b = p;
  107.         break;
  108.         case 2 : 
  109.         *r = p;
  110.         *g = v;
  111.         *b = t;
  112.         break;
  113.         case 3 : 
  114.         *r = p;
  115.         *g = q;
  116.         *b = v;
  117.         break;
  118.         case 4 : 
  119.         *r = t;
  120.         *g = p;
  121.         *b = v;
  122.         break;
  123.         case 5 : 
  124.         *r = v;
  125.         *g = p;
  126.         *b = q;
  127.         break;
  128.     }
  129.     }
  130. }
  131.  
  132. GLubyte rainbow[4*stripeImageWidth];
  133. void makeRainbow(void) {
  134.    int j;
  135.    for (j = 0; j < stripeImageWidth; j++) {
  136.       float r, g, b;
  137.       hsv_to_rgb((float)j/(stripeImageWidth-1.f), 1.0, 1.0, &r, &g, &b);
  138.       rainbow[4*j] = r*255;
  139.       rainbow[4*j+1] = g*255;
  140.       rainbow[4*j+2] = b*255;
  141.       rainbow[4*j+3] = (GLubyte) 255;
  142.    }
  143. }
  144.  
  145. /*  planes for texture coordinate generation  */
  146. static GLfloat xequalzero[] = {1.0, 0.0, 0.0, 0.0};
  147. static GLfloat slanted[] = {1.0, 1.0, 1.0, 0.0};
  148. static GLfloat *currentCoeff;
  149. static GLenum currentPlane;
  150. static GLint currentGenMode;
  151.  
  152. void init(void) {
  153.    glClearColor (0.0, 0.0, 0.0, 0.0);
  154.    glEnable(GL_DEPTH_TEST);
  155.    glShadeModel(GL_SMOOTH);
  156.  
  157.    makeStripeImage();
  158.    makeRainbow();
  159.    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  160.  
  161.    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  162.    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  163.    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  164.    glTexImage1D(GL_TEXTURE_1D, 0, 4, stripeImageWidth, 0,
  165.                 GL_RGBA, GL_UNSIGNED_BYTE, stripeImage);
  166.  
  167.    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  168.    currentCoeff = xequalzero;
  169.    currentGenMode = GL_OBJECT_LINEAR;
  170.    currentPlane = GL_OBJECT_PLANE;
  171.    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
  172.    glTexGenfv(GL_S, currentPlane, currentCoeff);
  173.  
  174.    glEnable(GL_TEXTURE_GEN_S);
  175.    glEnable(GL_TEXTURE_1D);
  176.    glEnable(GL_CULL_FACE);
  177.    glEnable(GL_LIGHTING);
  178.    glEnable(GL_LIGHT0);
  179.    glEnable(GL_AUTO_NORMAL);
  180.    glEnable(GL_NORMALIZE);
  181.    glFrontFace(GL_CW);
  182.    glCullFace(GL_BACK);
  183.    glMaterialf (GL_FRONT, GL_SHININESS, 64.0);
  184. }
  185.  
  186. void tfunc(void) {
  187.     static int state;
  188.     if (state ^= 1) {
  189.     glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  190.     glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  191.     glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  192.     glTexImage1D(GL_TEXTURE_1D, 0, 4, stripeImageWidth, 0,
  193.                 GL_RGBA, GL_UNSIGNED_BYTE, rainbow);
  194.     } else {
  195.     glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  196.     glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  197.     glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  198.     glTexImage1D(GL_TEXTURE_1D, 0, 4, stripeImageWidth, 0,
  199.                 GL_RGBA, GL_UNSIGNED_BYTE, stripeImage);
  200.     }
  201.     glutPostRedisplay();
  202. }
  203.  
  204. void display(void) {
  205. #if 0
  206.     static GLUquadricObj *q = NULL;
  207. #endif
  208.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  209.  
  210.    glPushMatrix();
  211.    glTranslatef(0., 0., transx);
  212.    glRotatef(rotx, 1.0, 0.0, 0.0);
  213.    glRotatef(45.0, 0.0, 0.0, 1.0);
  214.    glutSolidTeapot(2.0);
  215. #if 0
  216.    if (!q) q = gluNewQuadric();
  217.     gluQuadricTexture(q, GL_TRUE);
  218.    gluCylinder(q, 1.0, 2.0, 3.0, 10, 10);
  219. #endif
  220.    glPopMatrix();
  221.    glutSwapBuffers();
  222. }
  223.  
  224. void reshape(int w, int h) {
  225.    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  226.    glMatrixMode(GL_PROJECTION);
  227.    glLoadIdentity();
  228.    if (w <= h)
  229.       glOrtho (-3.5, 3.5, -3.5*(GLfloat)h/(GLfloat)w, 
  230.                3.5*(GLfloat)h/(GLfloat)w, -3.5, 3.5);
  231.    else
  232.       glOrtho (-3.5*(GLfloat)w/(GLfloat)h, 
  233.                3.5*(GLfloat)w/(GLfloat)h, -3.5, 3.5, -3.5, 3.5);
  234.    glMatrixMode(GL_MODELVIEW);
  235.    glLoadIdentity();
  236. }
  237.  
  238. /*ARGSUSED1*/
  239. void keyboard (unsigned char key, int x, int y) {
  240.    switch (key) {
  241.       case 'e':
  242.       case 'E':
  243.          currentGenMode = GL_EYE_LINEAR;
  244.          currentPlane = GL_EYE_PLANE;
  245.          glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
  246.          glTexGenfv(GL_S, currentPlane, currentCoeff);
  247.          glutPostRedisplay();
  248.          break;
  249.       case 'o':
  250.       case 'O':
  251.          currentGenMode = GL_OBJECT_LINEAR;
  252.          currentPlane = GL_OBJECT_PLANE;
  253.          glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
  254.          glTexGenfv(GL_S, currentPlane, currentCoeff);
  255.          glutPostRedisplay();
  256.          break;
  257.       case 's':
  258.       case 'S':
  259.          currentCoeff = slanted;
  260.          glTexGenfv(GL_S, currentPlane, currentCoeff);
  261.          glutPostRedisplay();
  262.          break;
  263.       case 'x':
  264.       case 'X':
  265.          currentCoeff = xequalzero;
  266.          glTexGenfv(GL_S, currentPlane, currentCoeff);
  267.          glutPostRedisplay();
  268.          break;
  269.       case 't': tfunc(); break;
  270.       case 27:
  271.          exit(0);
  272.          break;
  273.       default:
  274.          break;
  275.    }
  276. }
  277.  
  278. int main(int argc, char*argv[]) {
  279.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  280.     glutInitWindowSize(256, 256);
  281.     glutInitWindowPosition(100, 100);
  282.     glutInit(&argc, argv);
  283.     glutCreateWindow(argv[0]);
  284.     init();
  285.     glutDisplayFunc(display);
  286.     glutReshapeFunc(reshape);
  287.     glutKeyboardFunc(keyboard);
  288.     glutMouseFunc(mouse);
  289.     glutMotionFunc(motion);
  290.     glutMainLoop();
  291.     return 0;
  292. }
  293.